/**
 * 列表
 */
role.roleGrid = role.roleGrid || Ext.extend(Ext.grid.GridPanel,{
	 initComponent:function(){
	 	var sm = new Ext.grid.CheckboxSelectionModel({singleSelect:true});
	 	var cm = new Ext.grid.ColumnModel([
        		 sm,new Ext.grid.RowNumberer(),
		         {header:'角色ID',dataIndex:"roleId", hidden:true},
		         {header:'角色',dataIndex:"roleName"},
		         {header:'角色描述',dataIndex:"roleDesc"}
			         
	    ]);
	    cm.defaultSortable = true;

	    var fields = [
				 {name:"roleId"},
				 {name:"roleName"},
				 {name:"roleDesc"}
		];
		var ds = new Ext.data.Store({
				proxy:new Ext.data.HttpProxy({
		        	method: 'POST',
		        	url:'role/getDataList.jhtml'
		        }),
		        reader:new Ext.data.JsonReader({
		            root:'rows',
		            totalProperty:'results',
		            fields: fields
		        }),
		        baseParams:{
		    	    start:0,
		            limit: 20
		        },
		        autoLoad:true
		});
	   Ext.apply(this, {
		 	store:ds,
            sm:sm,
            cm:cm,
            title:'角色列表',
            border:false,
    		stripeRows:true,
            loadMask: {msg:'正在加载数据，请稍候....'},
            bbar:new Ext.PagingToolbar({
                pageSize: 20,
                store:ds,
                displayInfo:true
            }),
            tbar:[],
            formWindow: new role.win(),
            authWindow: new role.authWin()
		 });
	 role.roleGrid.superclass.initComponent.call(this);
	 var builder = new ButtonBuilder(this,role.moduleId,'grid');
 	 builder.build(this.getTopToolbar());
	},
	//add
	addFn:function(){
		var addWin = this.ownerCt.ownerCt.formWindow;
		if(!addWin){
			addWin = new role.win();
		}
		addWin.setIconClass('add');															
		addWin.setTitle('添加角色');																	
		addWin.show();	
		addWin.findByType('form')[0].form.reset();
	},
	//edit
	editFn:function(){
		var grid = this.ownerCt.ownerCt;
		var record = grid.getSelectionModel().getSelected();			
		if (record == null) {Ext.Msg.alert('提示','请选择一条记录！');return;}	
		var editWin = grid.formWindow;
		if(!editWin){
			editWin = new role.win();
		}
	  	editWin.setIconClass('edit');														
	  	editWin.setTitle('编辑角色');																
	  	editWin.show();
	  	var win_form = editWin.findByType('form')[0];
	  	win_form.form.reset();												
	  	win_form.form.loadRecord(record);
	},
	//del
	delFn:function(button,v){
		var grid = this.ownerCt.ownerCt;
		var record = grid.getSelectionModel().getSelected();			
		if (record == null) {Ext.Msg.alert('提示','请选择一条记录！');return;}											
		Ext.Msg.confirm('提示', '确定要删除这条记录吗?', function (btn, text) {											
			if (btn == 'yes') {																				
			 var waiting = Ext.Msg.wait('正在处理，请稍等...', '', '');												
				Ext.Ajax.request({																				
					url:'role/delRoleByRoleId.jhtml',																		
					params:{roleId:record.data.roleId},																	
					method:'POST',																				
					success:function(response, options){														
						if(waiting!=null){waiting.hide();}														
						var json = Ext.decode(response.responseText);											
						if(json.success==true){																	
							Ext.Msg.alert('提示',json.msg,function(){												
								grid.getStore().reload();											
							});																					
						}else{																					
							Ext.Msg.alert('提示','发生异常！');														
						}																						
					},																							
					failure:function(response, options){														
						if(waiting!=null){waiting.hide();}														
						var json = Ext.decode(response.responseText);											
						Ext.Msg.alert('提示',json.msg==''?'发生异常！':json.msg);															
					}																							
				});																								
			}																									
		});		
	},
	authFn:function(){
		var grid = this.ownerCt.ownerCt;
		var record = grid.getSelectionModel().getSelected();			
		if (record == null) {Ext.Msg.alert('提示','请选择一条记录！');return;}	
		var authWin = this.ownerCt.ownerCt.authWindow;
		if(!authWin){
			authWin = new role.authWin();
		}
		authWin.setIconClass('auth');															
		authWin.setTitle('分配权限');
		authWin.items.get(0).root.reload();
		authWin.items.get(0).root.expand(true);
		authWin.show();
	},
	searchFn:function(){
		
	}
});

Ext.onReady(function(){
	var isDefer = function(){
		return   typeof(role.win) == 'undefined' ||
				 typeof(role.roleGrid) == 'undefuned';
	};
	var initFn = function(){
		var panel = new role.roleGrid();
		var tab = Ext.getCmp(role.parentId);
		tab.add(panel);
		tab.doLayout();
	};
	isDefer() ? initFn.defer(300) : initFn();
});